<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class  CaseCourtPersonnelController extends Controller
{

    /*
     * 添加律师
     * @return json
     * */
    public function add_court_personnel(Request $request){


        $data=$request->only('courtId','name');

        if(!preg_match('/^[0-9]{1,10}$/',$data['courtId']))  return ThrowApiExc('71002');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');

        $se = DB::table('C_courtPersonnel')
            ->where([
                "name"=>$data['name'],
                "courtId"=>$data["courtId"]
            ])
            ->first();
        if($se){
            return [
                'status_code'      => 41825,
                'message'     =>  '姓名重复',
                'data' => ""
            ];
        }

        $res = DB::table('C_courtPersonnel')->insertGetId(
            [
                'name'=>$data['name'],
                'courtId'=>$data['courtId'],
                'time'=> date('Y-m-d H:i:s', time())
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }

    /*
     * 修改律师
     * @return json
     * */
    public function update_court_personnel(Request $request,$cpid){

        $data=$request->only('courtId','status','name');
        if(!preg_match('/^[0-9]{1,10}$/',$data['courtId']))  return ThrowApiExc('71002');
        if(!preg_match('/^[0-9]{1,10}$/',$cpid))  return ThrowApiExc('71003');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');


        $se = DB::table('C_courtPersonnel')
            ->where([
                "name"=>$data['name'],
                "courtId"=>$data["courtId"]
            ])
            ->first();
        if($se){
            return [
                'status_code'      => 41825,
                'message'     =>  '姓名重复',
                'data' => ""
            ];
        }

        $res = DB::table('C_courtPersonnel')->where('id',$cpid)->update(
            [
                'name'=>$data['name'],
                'courtId'=>$data['courtId'],
                'time'=> date('Y-m-d H:i:s', time())
            ]
        );



        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }


    /*
     * 删除律师
     * @return json
     * */
    public function delete_court_personnel($cpid){


        $res=DB::table('C_courtPersonnel')->where('id',$cpid)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40003');
        }

    }


    /*
     * 查找律师
     * @return json
     * */
    public function select_court_personnel(Request $request,$courtId){

        $data=$request->only('name');
        $where=[['courtId','=',$courtId]];

        if(array_key_exists('name',$data))  $where[]=['name','like','%'.$data['name'].'%'];

        $data=DB::table('C_courtPersonnel')
            ->where($where)
            ->get();

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];

    }


}